package com.risetechnology.krystalkitchen1;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.risetechnology.common.Shared;
import com.risetechnology.data.Ingredient;
import com.risetechnology.data.Shopping;
import com.risetechnology.master.NavigationActivity;

public class ActivitySelectShoppingList extends NavigationActivity {

	private ListView listviewShopping;
	public static List<Shopping> shoppingList;
	private ListAdapter listAdapter;

	private LinearLayout layoutCreateNewList;
	private ArrayList<Ingredient> ingredients;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_select_shopping_list);

		setContents();
	}

	private void setContents() {
		ingredients = ActivityAddIngredients.ingredients;

		layoutCreateNewList = (LinearLayout) findViewById(R.id.layout_create_new_list);
		layoutCreateNewList.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showCreateNewShoppingListDialog();
			}
		});

		listviewShopping = (ListView) findViewById(R.id.lv_shopping_list);
		shoppingList = new ArrayList<Shopping>();
		listAdapter = new ListAdapter(ActivitySelectShoppingList.this, R.layout.list_item_select_shopping_list, shoppingList);
		listviewShopping.setAdapter(listAdapter);

		getShoppingList();
	}

	private void showCreateNewShoppingListDialog() {
		final Dialog dialog = new Dialog(ActivitySelectShoppingList.this.getParent());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_create_new_shopping_list);

		Window window = dialog.getWindow();
		WindowManager.LayoutParams wlp = window.getAttributes();

		wlp.gravity = Gravity.CENTER;
		wlp.width = WindowManager.LayoutParams.FILL_PARENT;
		wlp.flags &= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
		window.setAttributes(wlp);

		dialog.getWindow().setAttributes(wlp);

		dialog.show();

		final EditText edittextName = (EditText) dialog.findViewById(R.id.et_shopping_list_name);

		Button buttonOK = (Button) dialog.findViewById(R.id.btn_ok);
		buttonOK.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();

				String shoppingName = edittextName.getText().toString();
				Shopping shopping = new Shopping("", shoppingName);
				long id = Shared.dbAdapter.addShoppingList(shopping);
				
				for (Ingredient  ingredient : ingredients) {
					ingredient.setShopID((int)id);
					Shared.dbAdapter.addShoppingDetail(ingredient);
				}

				Toast.makeText(ActivitySelectShoppingList.this.getParent(), "Added ingredients to " + shoppingName, Toast.LENGTH_LONG).show();

				getShoppingList();
				
//				TabsManager.goToTab(3);
				onBackPressed();
			}
		});
	}


	private void getShoppingList() {
		if (ActivitySelectShoppingList.shoppingList != null) {
			ActivitySelectShoppingList.shoppingList.clear();
		}

		Shared.dbAdapter.getShoppingList(true);
		listAdapter.notifyDataSetChanged();
	}

	private class ListAdapter extends ArrayAdapter<Shopping> {

		private Context context;
		private int layoutId;
		private List<Shopping> data = null;

		public ListAdapter(Context context, int layoutId, List<Shopping> data) {
			super(context, layoutId, data);
			this.context = context;
			this.layoutId = layoutId;
			this.data = data;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(layoutId, parent, false);

				holder = new ViewHolder();

				holder.textViewName = (TextView) convertView.findViewById(R.id.tv_shopping_name);
				//holder.layoutDetails = (LinearLayout) convertView.findViewById(R.id.layout_details);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.textViewName.setText(data.get(position).getName());

			convertView.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					//Intent intent = new Intent(((NavigationActivity)context).getParent(), ActivityShoppingDetails.class);
					//intent.putExtra(Const.KEY_SHOPPING_NAME, data.get(position).getName());
					//((NavigationActivity)context).goNextHistory("Shopping Details", intent);
					
//					Shopping shopping = new Shopping(data.get(position).getId(), data.get(position).getName(), ingredients);
					
					for (Ingredient  ingredient : ingredients) {
						ingredient.setShopID(Integer.parseInt(data.get(position).getId()));
						Shared.dbAdapter.addShoppingDetail(ingredient);
					}
					
					
					
					Toast.makeText(ActivitySelectShoppingList.this.getParent(), 
							"Added ingredients to " + data.get(position).getName(), Toast.LENGTH_LONG).show();
					
//					TabsManager.goToTab(3);
					onBackPressed();
				}
			});

			return convertView;
		}
	}
	static class ViewHolder {
		TextView textViewName;
	}

}
